﻿using System;
using System.Collections.Generic;
using System.Text;
using VideoCache.Aggregation.Interfaces;
using Fsb.Utility;

namespace VideoCache.Aggregation.Implements
{
    [Site("56.com")]
    class _56Fetcher
    {
        public string GetToken(string url)
        {
            string token = this.GetTokenByUrl(url);

            if (string.IsNullOrEmpty(token))
            {
                return this.GetTokenByPlayer(url);
            }

            return token;
        }

        private string GetTokenByUrl(string url)
        {
            string html = HttpUtility.Get(url, Encoding.UTF8);

            string json = StringUtility.SubString(html, "var _oFlv_o =", ";");

            Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(json);

            if (jObject != null)
            {
                return jObject["id"].ToString().Trim('\"');
            }

            return null;
        }

        private string GetTokenByPlayer(string url)
        {
            // &img_host=v155.56.com&host=c92.56.com&pURL=24&sURL=25&user=eindown2009&user_id=eindown2009&URLid=sc_134915452999hd&tag=我是特种兵,电视剧,军旅,吴京,侯勇,我是特种兵之利刃出鞘第01集&channel=&Subject=我是特种兵之利刃出鞘第01集&totaltimes=2499188&key=728114583402d590b59838f9008c0d72&effectID=0&flvid=74123223&start_sec=0&opera_id=-4&loading_deco_version=off&56.swf&statid=c&now_user=&show_mode=normal&over_panel_auto_play_next_time=8&show_mode_general=0

            return UrlHelper.GetParameter(url, "flvid");
        }
    }
}
